package com.wt.javaConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import java.util.Properties;

/**
 * Author hiberk
 * Date 2017/6/27 10:51
 * 用java代码代替xml配置
 */
@Configuration
@EnableWebMvc
public class JavaConfig extends WebMvcConfigurerAdapter{

//    @Bean
//    public HandlerExceptionResolver exceptionHandler() {
//        class ExceptionResolver implements HandlerExceptionResolver {
//            @Override
//            public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
//                Map<String,Object> model = new HashMap<String,Object>();
//                model.put("ex", e);
//                return new ModelAndView("error",model);
//            }
//        }
//        return new ExceptionResolver();
//    }

    @Bean(name="simpleMappingExceptionResolver")
    public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() {
        SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver();

        Properties mappings = new Properties();
        mappings.setProperty("DatabaseException", "error");
        mappings.setProperty("InvalidCreditCardException", "error");

        r.setExceptionMappings(mappings);  // 默认为空
        r.setDefaultErrorView("error");    // 默认没有
        r.setExceptionAttribute("ex");
        /*设置日志输出级别，不定义则默认不输出警告等错误日志信息*/
        r.setWarnLogCategory("example.MvcLogger");
        return r;
    }
}
